﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using diploma.core.data.objects;
using diploma.core.data;
using diploma.test.tools;

namespace diploma.test.core
{
    public class TermQuestion : Question
    {
        private int termCount = 0;
        private const int EMPTY_TERM_COUNT = 2;
        private string term = "";
        private List<string> hiding;
        public string Term
        {
            get
            {
                return term;
            }
        }
        public int TermCount
        {
            get
            {
                if (termCount > 0)
                {
                    return termCount;
                }
                else
                {
                    return EMPTY_TERM_COUNT;
                }
            }
        }

        public TermQuestion(string _question, string _term, DataList<IDataObject> _terms, int _termCount)
            : base(_question)
        {
            this.term = _term;
            this.termCount = _termCount;
            hiding = new List<string>();
            int i = 0;
            List<IDataObject> terms = TestTools.ReOrganizeList<IDataObject>(_terms.ToList());
            string[] m = term.ToLower().Trim().Split(new char[] { ' ', ',', '.', '?', '!' });
            foreach (Term t in _terms)
            {
                if (i >= 2) break;
                foreach (string s in t.Variants)
                {
                    if (m.Contains<string>(s.Trim().ToLower()))
                    {
                        term = term.ToLower().Replace(s.Trim().ToLower(), "__" + (++i) + "__");
                        hiding.Add(s.Trim().ToLower());
                        continue;
                    }
                }
                if (m.Contains<string>(t.Title.Trim().ToLower()))
                {
                    term = term.Replace(t.Title.Trim().ToLower(), "__" + (++i) + "__ ");
                    hiding.Add(t.Title.Trim().ToLower());
                    continue;
                }
                
            }
            termCount = i;
        }

        public override QuestionTypes Type
        {
            get
            {
                return QuestionTypes.Term;
            }
        }

        public string[] RightAnswer
        {
            get
            {
                return hiding.ToArray();
            }
        }
    }
}
